package calcul;

import calcul.operation.*;

public class Parser {

	public Parser() {
	}

	public double parse(String str) throws Exception {
		
		try{
		String[] tab = str.split(" ");
		
		if(tab[1].equals("+")) {
			double n1 = Double.valueOf(tab[0]);
			double n2 = Double.valueOf(tab[2]);
			Operator op = new Add();
			return op.compute(n1,n2);
		}
		else if(tab[1].equals("*") || tab[1].equals("x")) {
			double n1 = Double.valueOf(tab[0]);
			double n2 = Double.valueOf(tab[2]);
			Operator op = new Mult();
			return op.compute(n1,n2);
		}
		else if(tab[1].equals("-")) {
			double n1 = Double.valueOf(tab[0]);
			double n2 = Double.valueOf(tab[2]);
			Operator op = new Sub();
			return op.compute(n1,n2);
		}
		else if(tab[1].equals("/") || tab[1].equals(":")) {
			double n1 = Double.valueOf(tab[0]);
			double n2 = Double.valueOf(tab[2]);
			Operator op = new Div();
			return op.compute(n1,n2);
		}
		else if(tab[0].equalsIgnoreCase("circ")){
			double n1 = Double.valueOf(tab[1]);
			if(n1 < 0){
				throw new IllegalArgumentException();
			}
			Operator op = new Circle();
			return op.compute(n1, 0);
		}
		else if(tab[0].equalsIgnoreCase("rect")){
			double n1 = Double.valueOf(tab[1]);
			double n2 = Double.valueOf(tab[2]);
			if(n1<0 || n2 < 0){
				throw new IllegalArgumentException();
			}
			Operator op = new Rectangle();
			return op.compute(n1, n2);
		}
		else if(tab[0].equalsIgnoreCase("tria")){
			double n1 = Double.valueOf(tab[1]);
			double n2 = Double.valueOf(tab[2]);
			if(n1<0 || n2 < 0){
				throw new IllegalArgumentException();
			}
			Operator op = new Triangle();
			return op.compute(n1, n2);
		}
		else
			throw new Exception("Invalid Input");
		}
		catch (ArrayIndexOutOfBoundsException e) {
		throw new Exception("Invalid Input");
		}
		
	}
}
